package com.lin.generate.config.datasource;

import lombok.extern.slf4j.Slf4j;

/**
 * @description TODO
 * @Author: z156940l@outlook.com
 * @className DynamicDataSourceContextHolder
 * @Date: 2023/6/1 23:21
 */
@Slf4j
public class DynamicDataSourceContextHolder {

    /**
     * 默认数据源
     */
    public static final String DEFAULT_DS = DataSourceNameList.MASTER_DATASOURCE.getDatasourceName();

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    private static final ThreadLocal<String> userHolder = new ThreadLocal<>();

    // 设置数据源名
    public static void setDB(String dbType) {
        log.info("切换到{" + dbType + "}数据源");
        contextHolder.set(dbType);
    }

    // 获取数据源名
    public static String getDB() {
        return (contextHolder.get());
    }

    // 清除数据源名
    public static void clearDB() {
        contextHolder.remove();
    }
    /**
     * 设置用户ID
     * @param compKey
     */
    public static void setCompany(String compKey) {
        userHolder.set(compKey);
    }

    /**
     * 获取用户ID
     * @return
     */
    public static String getUserId() {
        return userHolder.get();
    }

    /**
     * 清除
     */
    public static void clearCompany() {
        userHolder.remove();
    }
}
